package com.sg.service.biz.framework.impl;

import cn.hutool.core.bean.BeanUtil;
import com.sg.common.util.RedisUtil;
import com.sg.dto.base.framework.req.QueryCheckItemStatDetailReq;
import com.sg.dto.base.framework.req.QueryCheckItemStatReq;
import com.sg.dto.base.framework.req.QueryOrgDetailReq;
import com.sg.dto.biz.framework.req.*;
import com.sg.dto.biz.framework.res.*;
import com.sg.entity.Organization;
import com.sg.entity.StatusCheckItem;
import com.sg.service.base.framework.MOrganizationService;
import com.sg.service.base.framework.MStatusCheckItemService;
import com.sg.common.exception.Assert;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @date ：
 */
@Service
public class OffStateServiceImpl
        implements com.wicket.okrframework.biz.service.OffStateService {

    @Resource
    private RedisUtil redisUtil;
    @Resource
    private MStatusCheckItemService mStatusCheckItemService;
    @Resource
    private MOrganizationService mOrganizationService;

    /**
     * D1-1修改状态校验项[947]
     * gen by moon at 2022/7/3 下午5:23:07
     */
    @Trace(operationName = "D1-1修改状态校验项")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public UpdateCheckItemStatRespDto updateCheckItemStat(UpdateCheckItemStatReqDto reqDto) {

        // TODO: 2022/7/19 胡睿哲:手动修改
        QueryCheckItemStatDetailReq statusCheckItem_1 = new QueryCheckItemStatDetailReq();
        if (reqDto != null) {
            statusCheckItem_1.setCheckItemCode(reqDto.getCheckItemCode());//sourceId:19217_1
            statusCheckItem_1.setObjectTypeCode(reqDto.getObjectTypeCode());//sourceId:19218_1
            statusCheckItem_1.setEntityId(reqDto.getEntityId());//sourceId:19219_1
        }

        StatusCheckItem statusCheckItemResp = mStatusCheckItemService.queryCheckItemStatDetail(statusCheckItem_1);


        //步骤0: 1-1-22修改状态校验项	 - updateCheckItemStat
        boolean bOOLEAN;
        StatusCheckItem statusCheckItem_2 = new StatusCheckItem();
        if (reqDto != null) {
            statusCheckItem_2.setStatusCheckItemId(statusCheckItemResp.getStatusCheckItemId());//sourceId:19216_1
            statusCheckItem_2.setIsComplete(reqDto.getIsComplete());
        }

        /*1-1-22修改状态校验项	[325]   */
        bOOLEAN = mStatusCheckItemService.updateCheckItemStat(statusCheckItem_2);


        UpdateCheckItemStatRespDto retData = new UpdateCheckItemStatRespDto();


        return retData;
    }

    /**
     * D1-1查询状态校验项列表[960]
     */
    @Trace(operationName = "D1-1查询状态校验项列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public QueryCheckItemStatListRespDto queryCheckItemStatList(QueryCheckItemStatListReqDto reqDto) {


        //步骤0: 1-1-22查询状态校验项列表 - queryCheckItemStat
        List<StatusCheckItem> listStatusCheckItem = null;
        QueryCheckItemStatReq queryCheckItemStatReq = new QueryCheckItemStatReq();
        if (reqDto != null) {
            queryCheckItemStatReq.setCheckItemCode(reqDto.getCheckItemCode());//sourceId:19700_1
            queryCheckItemStatReq.setObjectTypeCode(reqDto.getObjectTypeCode());//sourceId:19701_1
            queryCheckItemStatReq.setEntityId(reqDto.getEntityId());//sourceId:19702_1
        }

        /*1-1-22查询状态校验项列表[328]   */
        listStatusCheckItem = mStatusCheckItemService.queryCheckItemStat(queryCheckItemStatReq);


        QueryCheckItemStatListRespDto retData = new QueryCheckItemStatListRespDto();
        retData.setCheckItemStatList(listStatusCheckItem.stream().map(item -> BeanUtil.toBean(item, CheckItemStatDto.class)).collect(Collectors.toList()));//sourceId:19707_1


        return retData;
    }

    /**
     * D1-1查询状态校验项详情[954]
     * gen by moon at 10/2/2022, 7:51:57 AM
     */
    @Trace(operationName = "D1-1查询状态校验项详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public QueryCheckItemStatDetailRespDto queryCheckItemStatDetail(QueryCheckItemStatDetailReqDto reqDto) {


        StatusCheckItem statusCheckItem_1 = null;
//步骤0: 1-1-22查询状态校验项详情 - queryCheckItemStatDetail
        StatusCheckItem statusCheckItem = null;
        QueryCheckItemStatDetailReq queryCheckItemStatDetailReq = new QueryCheckItemStatDetailReq();
        if (reqDto != null) {
            queryCheckItemStatDetailReq.setCheckItemCode(reqDto.getCheckItemCode());//SimpleFieldAssign//sourceId:19283_1
            queryCheckItemStatDetailReq.setObjectTypeCode(reqDto.getObjectTypeCode());//SimpleFieldAssign//sourceId:19284_1
            queryCheckItemStatDetailReq.setEntityId(reqDto.getEntityId());//SimpleFieldAssign//sourceId:19285_1
        }

        /*1-1-22查询状态校验项详情[327]   */

        statusCheckItem = mStatusCheckItemService.queryCheckItemStatDetail(queryCheckItemStatDetailReq);


        statusCheckItem_1 = statusCheckItem;

        QueryCheckItemStatDetailRespDto retData = new QueryCheckItemStatDetailRespDto();
        if (statusCheckItem_1 != null) {
            retData.setIsComplete(statusCheckItem_1.getIsComplete());//SimpleFieldAssign//sourceId:19331_1
        }


        return retData;
    }

    /**
     * D1-1修改检查项状态(公共)[947]
     * gen by moon at 10/1/2022, 11:49:23 PM
     */
    @Trace(operationName = "D1-1修改检查项状态(公共)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public UpdateCheckItemStatComRespDto updateCheckItemStatCom(UpdateCheckItemStatComReqDto reqDto) {


        //步骤0: 1-1-22查询状态校验项详情 - queryCheckItemStatDetail
        StatusCheckItem statusCheckItem = null;
        QueryCheckItemStatDetailReq queryCheckItemStatDetailReq = new QueryCheckItemStatDetailReq();
        if (reqDto != null) {
            queryCheckItemStatDetailReq.setEntityId(reqDto.getEntityId());//SimpleFieldAssign//sourceId:143246_1
            queryCheckItemStatDetailReq.setObjectTypeCode(reqDto.getObjectTypeCode());//SimpleFieldAssign//sourceId:143245_1
            queryCheckItemStatDetailReq.setCheckItemCode(reqDto.getCheckItemCode());//SimpleFieldAssign//sourceId:143181_1
        }

        /*1-1-22查询状态校验项详情[327]   */
        Assert.isNull(queryCheckItemStatDetailReq.getEntityId(), "D1-1修改检查项状态(公共)-1-1-22查询状态校验项详情-内容对象实例ID不能为空", false);
        Assert.isNull(queryCheckItemStatDetailReq.getObjectTypeCode(), "D1-1修改检查项状态(公共)-1-1-22查询状态校验项详情-内容对象类型编码不能为空", false);
        Assert.isNull(queryCheckItemStatDetailReq.getCheckItemCode(), "D1-1修改检查项状态(公共)-1-1-22查询状态校验项详情-检查项标识不能为空", false);
        statusCheckItem = mStatusCheckItemService.queryCheckItemStatDetail(queryCheckItemStatDetailReq);


//步骤1: 1-1-22修改状态校验项	 - updateCheckItemStat
        boolean bOOLEAN;
        if (statusCheckItem != null) {
            StatusCheckItem statusCheckItem_2 = new StatusCheckItem();
            if (statusCheckItem != null) {
                statusCheckItem_2.setStatusCheckItemId(statusCheckItem.getStatusCheckItemId());//SimpleFieldAssign//sourceId:19216_1
            }
            if (reqDto != null) {
                statusCheckItem_2.setIsComplete(reqDto.getIsComplete());//SimpleFieldAssign//sourceId:19220_1
            }

            /*1-1-22修改状态校验项	[325]   */
            Assert.isNull(statusCheckItem_2.getStatusCheckItemId(), "D1-1修改检查项状态(公共)-1-1-22修改状态校验项	-状态校验项ID不能为空", false);
            Assert.isNull(statusCheckItem_2.getIsComplete(), "D1-1修改检查项状态(公共)-1-1-22修改状态校验项	-是否已完成不能为空", false);
            bOOLEAN = mStatusCheckItemService.updateCheckItemStat(statusCheckItem_2);


        }

        UpdateCheckItemStatComRespDto retData = new UpdateCheckItemStatComRespDto();


        return retData;
    }

    /**
     * D1-3更新组织成员管理检查项等[4184]
     * gen by moon at 10/2/2022, 6:00:29 AM
     */
    @Trace(operationName = "D1-3更新组织成员管理检查项等")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public RefreshOrgMemberMgtCheckEtcRespDto refreshOrgMemberMgtCheckEtc(RefreshOrgMemberMgtCheckEtcReqDto reqDto) {


        //virtualUsage 1-3-12查询组织详情  14569
        Organization organization = null;
        QueryOrgDetailReq queryOrgDetailReq = new QueryOrgDetailReq();
        queryOrgDetailReq.setSubjectLifeCycle("CURRENTLY_VALID");//sourceId:257083_1_14569
        if (reqDto != null) {
            queryOrgDetailReq.setOrganizationId(reqDto.getOrganizationId());//SimpleFieldAssign//sourceId:257063_1_14569
        }

        /*1-3-12查询组织详情[201]   */
        Assert.isNull(queryOrgDetailReq.getOrganizationId(), "D1-3更新组织成员管理检查项等-1-3-12查询组织详情-组织ID不能为空", false);
        Assert.isNull(queryOrgDetailReq.getSubjectLifeCycle(), "D1-3更新组织成员管理检查项等-1-3-12查询组织详情-主体生命周期不能为空", false);
        organization = mOrganizationService.queryOrgDetail(queryOrgDetailReq);
        Assert.isTrue(organization == null || organization.getOrganizationId() == null, "返回值不能为空", false);


//virtualUsage 1-1-22查询成员管理检查项  14573
        StatusCheckItem statusCheckItem = null;
        if (organization != null) {
            QueryCheckItemStatDetailReq queryCheckItemStatDetailReq = new QueryCheckItemStatDetailReq();
            queryCheckItemStatDetailReq.setObjectTypeCode("ORG_CHECK");//sourceId:257050_1_14573
            queryCheckItemStatDetailReq.setCheckItemCode("ORG_MEMBER_CHECK");//CUSTOM_CONVENTION//sourceId:257049_1_14573
            if (organization != null) {
                queryCheckItemStatDetailReq.setEntityId(organization.getOrganizationId());//SimpleFieldAssign//sourceId:257051_1_14573
            }

            /*1-1-22查询成员管理检查项[327]   */
            Assert.isNull(queryCheckItemStatDetailReq.getEntityId(), "D1-3更新组织成员管理检查项等-1-1-22查询成员管理检查项-内容对象实例ID不能为空", false);
            Assert.isNull(queryCheckItemStatDetailReq.getObjectTypeCode(), "D1-3更新组织成员管理检查项等-1-1-22查询成员管理检查项-内容对象类型编码不能为空", false);
            Assert.isNull(queryCheckItemStatDetailReq.getCheckItemCode(), "D1-3更新组织成员管理检查项等-1-1-22查询成员管理检查项-检查项标识不能为空", false);
            statusCheckItem = mStatusCheckItemService.queryCheckItemStatDetail(queryCheckItemStatDetailReq);
            Assert.isTrue(statusCheckItem == null || statusCheckItem.getStatusCheckItemId() == null, "返回值不能为空", false);


        }
//virtualUsage 1-1-22查询组织架构管理检查项  14574
        StatusCheckItem statusCheckItem_2 = null;
        if (organization != null) {
            QueryCheckItemStatDetailReq queryCheckItemStatDetailReq_1 = new QueryCheckItemStatDetailReq();
            queryCheckItemStatDetailReq_1.setObjectTypeCode("ORG_CHECK");//sourceId:257118_1_14574
            queryCheckItemStatDetailReq_1.setCheckItemCode("ORG_MGT_CHECK");//CUSTOM_CONVENTION//sourceId:257117_1_14574
            if (organization != null) {
                queryCheckItemStatDetailReq_1.setEntityId(organization.getOrganizationId());//SimpleFieldAssign//sourceId:257119_1_14574
            }

            /*1-1-22查询组织架构管理检查项[327]   */
            Assert.isNull(queryCheckItemStatDetailReq_1.getEntityId(), "D1-3更新组织成员管理检查项等-1-1-22查询组织架构管理检查项-内容对象实例ID不能为空", false);
            Assert.isNull(queryCheckItemStatDetailReq_1.getObjectTypeCode(), "D1-3更新组织成员管理检查项等-1-1-22查询组织架构管理检查项-内容对象类型编码不能为空", false);
            Assert.isNull(queryCheckItemStatDetailReq_1.getCheckItemCode(), "D1-3更新组织成员管理检查项等-1-1-22查询组织架构管理检查项-检查项标识不能为空", false);
            statusCheckItem_2 = mStatusCheckItemService.queryCheckItemStatDetail(queryCheckItemStatDetailReq_1);
            Assert.isTrue(statusCheckItem_2 == null || statusCheckItem_2.getStatusCheckItemId() == null, "返回值不能为空", false);


        }
        if ((reqDto != null && reqDto.getIsComplete() != null && reqDto.getIsComplete().equals("TRUE"))) {
            //if(D1-3更新组织成员管理检查项等.是否已完成 等于 是)  14571

            boolean bOOLEAN;
            if (statusCheckItem != null) {
                StatusCheckItem statusCheckItem_4 = new StatusCheckItem();
                if (statusCheckItem != null) {
                    statusCheckItem_4.setStatusCheckItemId(statusCheckItem.getStatusCheckItemId());//SimpleFieldAssign//sourceId:257084_1_14575
                }
                if (reqDto != null) {
                    statusCheckItem_4.setIsComplete(reqDto.getIsComplete());//SimpleFieldAssign//sourceId:257088_1_14575
                }

                /*1-1-22确认设置完成[325]   */
                Assert.isNull(statusCheckItem_4.getStatusCheckItemId(), "D1-3更新组织成员管理检查项等-1-1-22确认设置完成-状态校验项ID不能为空", false);
                Assert.isNull(statusCheckItem_4.getIsComplete(), "D1-3更新组织成员管理检查项等-1-1-22确认设置完成-是否已完成不能为空", false);
                bOOLEAN = mStatusCheckItemService.updateCheckItemStat(statusCheckItem_4);


            }
            boolean bOOLEAN_1;
            if (statusCheckItem_2 != null) {
                StatusCheckItem statusCheckItem_5 = new StatusCheckItem();
                if (statusCheckItem_2 != null) {
                    statusCheckItem_5.setStatusCheckItemId(statusCheckItem_2.getStatusCheckItemId());//SimpleFieldAssign//sourceId:257111_1_14576
                }
                if (reqDto != null) {
                    statusCheckItem_5.setIsComplete(reqDto.getIsComplete());//SimpleFieldAssign//sourceId:257115_1_14576
                }

                /*1-1-22取消设置完成[325]   */
                Assert.isNull(statusCheckItem_5.getStatusCheckItemId(), "D1-3更新组织成员管理检查项等-1-1-22取消设置完成-状态校验项ID不能为空", false);
                Assert.isNull(statusCheckItem_5.getIsComplete(), "D1-3更新组织成员管理检查项等-1-1-22取消设置完成-是否已完成不能为空", false);
                bOOLEAN_1 = mStatusCheckItemService.updateCheckItemStat(statusCheckItem_5);


            }
//processBranchName:正常结束 ,processBranchId:14577

        } else if ((reqDto != null && reqDto.getIsComplete() != null && reqDto.getIsComplete().equals("FALSE"))) {
            //elseif(D1-3更新组织成员管理检查项等.是否已完成 等于 否)  14578

            boolean bOOLEAN_2;
            if (statusCheckItem != null) {
                StatusCheckItem statusCheckItem_6 = new StatusCheckItem();
                if (statusCheckItem != null) {
                    statusCheckItem_6.setStatusCheckItemId(statusCheckItem.getStatusCheckItemId());//SimpleFieldAssign//sourceId:257111_1_14584
                }
                if (reqDto != null) {
                    statusCheckItem_6.setIsComplete(reqDto.getIsComplete());//SimpleFieldAssign//sourceId:257115_1_14584
                }

                /*1-1-22取消设置完成[325]   */
                Assert.isNull(statusCheckItem_6.getStatusCheckItemId(), "D1-3更新组织成员管理检查项等-1-1-22取消设置完成-状态校验项ID不能为空", false);
                Assert.isNull(statusCheckItem_6.getIsComplete(), "D1-3更新组织成员管理检查项等-1-1-22取消设置完成-是否已完成不能为空", false);
                bOOLEAN_2 = mStatusCheckItemService.updateCheckItemStat(statusCheckItem_6);


            }
            boolean bOOLEAN_3;
            if (statusCheckItem_2 != null) {
                StatusCheckItem statusCheckItem_7 = new StatusCheckItem();
                if (statusCheckItem_2 != null) {
                    statusCheckItem_7.setStatusCheckItemId(statusCheckItem_2.getStatusCheckItemId());//SimpleFieldAssign//sourceId:257111_1_14585
                }
                if (reqDto != null) {
                    statusCheckItem_7.setIsComplete(reqDto.getIsComplete());//SimpleFieldAssign//sourceId:257115_1_14585
                }

                /*1-1-22取消设置完成[325]   */
                Assert.isNull(statusCheckItem_7.getStatusCheckItemId(), "D1-3更新组织成员管理检查项等-1-1-22取消设置完成-状态校验项ID不能为空", false);
                Assert.isNull(statusCheckItem_7.getIsComplete(), "D1-3更新组织成员管理检查项等-1-1-22取消设置完成-是否已完成不能为空", false);
                bOOLEAN_3 = mStatusCheckItemService.updateCheckItemStat(statusCheckItem_7);


            }
//processBranchName:正常结束 ,processBranchId:14582

        }
        RefreshOrgMemberMgtCheckEtcRespDto retData = new RefreshOrgMemberMgtCheckEtcRespDto();


        return retData;
    }
    //
}
